<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/center/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/center/css/public.css}" media="all">
	<script th:src="@{/center/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
	<script th:src="@{/center/js/config.js}" charset="utf-8"></script>
	<script th:inline="none">
	    layui.use(['form', 'table', 'laydate'], function () {
	        var $ = layui.jquery,
	            form = layui.form,
	            table = layui.table,
	            laydate = layui.laydate;
	
	        laydate.render({
	            elem: '#centerCreatetime',
	            type: 'datetime',
	            format: 'yyyy-MM-dd HH:mm:ss',
	            max: 'new Date()'
	        });
	        //查所有记录，带分页(注意查所有和多条件查询的传参和分页，前后端处理方式都不一样)
	        var tableIns = table.render({
	            elem: '#currentTableId',
	            url: '/center/showAllLog',
	            method: 'post',
	            parseData: function (res) {
	                return {
	                    "code": res.code,
	                    "msg": res.msg,
	                    "count": res.count,
	                    "data": res.data[0].list
	                }
	            },
	            initSort: {field: 'centerCreatetime', type: 'desc'},
	            toolbar: '#toolbarDemo',
	            defaultToolbar: ['filter', 'exports', 'print', {
	                title: '提示',
	                layEvent: 'LAYTABLE_TIPS',
	                icon: 'layui-icon-tips'
	            }],
	            cols: [[
	                {type: "checkbox", width: 50},
	                {field: 'centerUsername', width: 200, title: '用户名', sort: true},
	                {field: 'centerOperation', width: 200, title: '操作关键字', sort: true},
	                {field: 'centerMethod', width: 150, title: '调用方法', sort: true},
	                {field: 'centerParams', width: 80, title: '参数列表', sort: true},
	                {field: 'centerIp', width: 150, title: 'ip地址', sort: true},
	                {field: 'centerCreatetime', width: 200, title: '操作时间', sort: true},
	                {field: 'centerLogid', width: 100, title: '日志ID', sort: true},
	                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
	            ]],
	            limit: 10,
	            limits: [10, 15, 20, 25, 50, 100],
	            page: true,
	            skin: 'row'
	
	
	        });
	        // 监听搜索操作
	        //多条件查询，带分页
	        form.on('submit(data-search-btn)', function (data) {
	            table.render({
	                elem: '#currentTableId',
	                url: '/center/showLogByCondition',
	                method: "post",
	                where: {
	                    name: data.field.centerUsername,
	                    id: data.field.centerLogid,
	                    date: data.field.centerCreatetime,
	                    op: data.field.centerOperation,
	                    ip: data.field.centerIp
	                },
	                parseData: function (res) {
	                    return {
	                        "code": res.code,
	                        "msg": res.msg,
	                        "count": res.count,
	                        "data": res.data[0].list
	                    }
	                },
	                initSort: {field: 'centerCreatetime', type: 'desc'},
	                toolbar: '#toolbarDemo',
	                defaultToolbar: ['filter', 'exports', 'print', {
	                    title: '提示',
	                    layEvent: 'LAYTABLE_TIPS',
	                    icon: 'layui-icon-tips'
	                }],
	                cols: [[
	                    {type: "checkbox", width: 50},
	                    {field: 'centerUsername', width: 100, title: '用户名', sort: true},
	                    {field: 'centerOperation', width: 200, title: '操作关键字', sort: true},
	                    {field: 'centerMethod', width: 150, title: '调用方法', sort: true},
	                    {field: 'centerParams', width: 80, title: '参数列表', sort: true},
	                    {field: 'centerIp', width: 150, title: 'ip地址', sort: true},
	                    {field: 'centerCreatetime', width: 200, title: '操作时间', sort: true},
	                    {field: 'centerLogid', width: 100, title: '日志ID', sort: true},
	                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
	                ]],
	                limit: 10,
	                limits: [10, 15, 20, 25, 50, 100],
	                page: true,
	                skin: 'row'
	
	
	            });
	
	
	            return false;
	        });
	
	
	        /**
	         * toolbar监听事件
	         */
	
	        table.on('toolbar(currentTableFilter)', function (obj) {
	
	            if (obj.event === 'delete') {  // 监听删除操作
	                //获取复选框选中的数据
	                var checkStatus = table.checkStatus('currentTableId');
	                var data = checkStatus.data;
	                if (data.length > 0) {
	                    var arr = [];
	                    //只要centerLogid的字符串数组
	                    for (var i = 0; i < data.length; i++) {
	
	                        arr.push(data[i].centerLogid);
	                    }
	
	                    var jsonstr = JSON.stringify(arr);
	
	                    layer.confirm('是否确定批量删除：' + arr.length + '条日志记录', function (index) {
	                        //发起批量删除请求
	                        $.ajax({
	                            url: '/center/batchDeleteLogManually',
	                            contentType: 'application/json;charset=utf-8',
	                            type: 'POST',
	                            data: jsonstr,
	                            success: function (res) {
	                                layer.alert(res.msg + res.count + '条记录');
	                                tableIns.reload();
	                            }
	                        });
	                        layer.close(index);
	
	                    });
	                } else {
	                    layer.alert('请勾选需要删除的记录');
	                }
	
	
	            }
	        });
	
	
	        //监听表格复选框选择
	        table.on('checkbox(currentTableFilter)', function (obj) {
	
	            console.log(obj.data.centerLogid);
	
	        });
	
	        table.on('tool(currentTableFilter)', function (obj) {
	            var data = obj.data;
	            var jsonstr = data.centerLogid;
	            if (obj.event === 'delete') {
	                layer.confirm('是否确定删除ID为：' + jsonstr + '的日志记录', function (index) {
	                    $.ajax({
	                        url: '/center/deleteLogManually',
	                        contentType: 'application/json;charset=utf-8',
	                        method: 'POST',
	                        data: jsonstr,
	                        success: function (res) {
	                            layer.alert(res.msg + res.count + '条记录');
	                            tableIns.reload();
	                        }
	                    });
	
	                    layer.close(index);
	                });
	            }
	        });
	
	    });
	</script>
	
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索日志</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="centerUsername" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">ip地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="centerIp" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">操作关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="centerOperation" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">操作日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="centerCreatetime" id="centerCreatetime" autocomplete="off"
                                       class="layui-input" placeholder="指定日期到当前时间的日志">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">日志ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="centerLogid" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

</body>
</html>